package com.xt.partjobback.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.partjobback.constant.CommonConstant;
import com.xt.partjobback.constant.ResultDTO;
import com.xt.partjobback.dto.JobDto;
import com.xt.partjobback.entity.Job;
import com.xt.partjobback.service.ClassIfyService;
import com.xt.partjobback.service.JobService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;

/**
 * @author leotong
 */
@RestController
@RequestMapping(CommonConstant.API_URI_VERSION_V1+"/job")
public class JobController {
    private final JobService jobService;

    private final ClassIfyService classIfyService;

    @Autowired
    public JobController(JobService jobService,ClassIfyService classIfyService) {
        this.jobService = jobService;
        this.classIfyService=classIfyService;
    }

    @PostMapping("/update")
    public ResultDTO<Boolean> updateJob(@RequestBody Job job){
        return ResultDTO.success(jobService.updateById(job));
    }

    @GetMapping("/delete")
    public ResultDTO<Boolean> deleteJob(Integer id){
        return ResultDTO.success(jobService.removeById(id));
    }

    @PostMapping("/getJob")
    public ResultDTO<Page<JobDto>> getJob(@RequestBody Job job){
        Page<Job> jobByCondition = jobService.getJobByCondition(job);
        Page<JobDto> result=new Page<>();
        result.setTotal(jobByCondition.getTotal());
        result.setRecords(new ArrayList<>());
        for (Job job1 : jobByCondition.getRecords()) {
            JobDto jobDto=new JobDto();
            BeanUtils.copyProperties(job1,jobDto);
            jobDto.setUseCollect(false);
            jobDto.setClassify(classIfyService.getById(job1.getCid()));
            result.getRecords().add(jobDto);
        }
        return ResultDTO.success(result);
    }
    @PostMapping("/getJobByUser")
    public ResultDTO<Page<JobDto>> getJobByUser(@RequestBody Job job){
        return ResultDTO.success(jobService.getJobByUser(job));
    }

    @PostMapping("/createJob")
    public ResultDTO<Boolean> createJob(@RequestBody Job job){
        return ResultDTO.success(jobService.save(job));
    }
}
